package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.admin.bo.AdCreateBo;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.db.common.BasePageInfo;
import com.cskaoyan.market.db.common.CommonData;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.cskaoyan.market.util.ReflectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 小焦
 * @since 2023/03/22 14:35
 */
@Service
public class AdminAdServiceImpl implements AdminAdService {
	@Override
	public CommonData<MarketAd> list(BasePageInfo basePageInfo, String name, String content) {
		SqlSession sqlSession = MyBatisUtil.getSession();
		MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);

		PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
		MarketAdExample adExample = new MarketAdExample();
		MarketAdExample.Criteria criteria = adExample.createCriteria();
		criteria.andDeletedEqualTo(false);
		if (!StringUtils.isEmpty(name)) {
			criteria.andNameLike("%" + name.trim() + "%");
		}
		if (!StringUtils.isEmpty(content)) {
			criteria.andContentLike("%" + content.trim() + "%");
		}
		adExample.setOrderByClause(basePageInfo.getSort()+ " " + basePageInfo.getOrder());
		List<MarketAd> marketAds = adMapper.selectByExample(adExample);
		sqlSession.commit();
		sqlSession.close();
		PageInfo<MarketAd> pageInfo = new PageInfo<>(marketAds);
		CommonData<MarketAd> data = CommonData.data(pageInfo);

		return data;
	}

	@Override
	public MarketAd create(AdCreateBo adCreateBo) {
		LocalDateTime now = LocalDateTime.now();
		MarketAd marketAd = ReflectUtils.copyObject(adCreateBo, MarketAd.class);
		marketAd.setDeleted(false);
		marketAd.setAddTime(now);
		marketAd.setUpdateTime(now);
		SqlSession sqlSession = MyBatisUtil.getSession();
		MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
		adMapper.insertSelective(marketAd);
		sqlSession.commit();
		sqlSession.close();
//        System.out.println(marketAd);
		return marketAd;
	}

	@Override
	public MarketAd update(MarketAd marketAd) {
		SqlSession sqlSession = MyBatisUtil.getSession();
		MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
		LocalDateTime now = LocalDateTime.now();
		marketAd.setUpdateTime(now);
		// 先修改 , 在查找
		int row = adMapper.updateByPrimaryKeySelective(marketAd);

		// 修改后在查找一下-- 这行代码可以不要
		// MarketAd data = adMapper.selectByPrimaryKey(marketAd.getId());
		sqlSession.commit();
		sqlSession.close();
		if (row < 1) {
			return null;
		}
		return marketAd;
	}

	@Override
	public int delete(AdCreateBo adDeleteVo) {
		SqlSession sqlSession = MyBatisUtil.getSession();
		MarketAdMapper adMapper = sqlSession.getMapper(MarketAdMapper.class);
		try {
			adMapper.logicalDeleteByPrimaryKey(adDeleteVo.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return 405;
		}
		sqlSession.commit();
		sqlSession.close();
		return 0;
	}
}
